package com.ubaer.zsycz.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.view.View;

import com.lsw.pullableview.PullToRefreshLayout;
import com.lsw.pullableview.PullableListView;
import com.ubaer.zsycz.R;
import com.ubaer.zsycz.inject.Constants;
import com.ubaer.zsycz.model.OrderModel;
import com.ubaer.zsycz.ui.activity.OrderDetailsActivity;
import com.ubaer.zsycz.ui.adapter.AlreadyProcessedAdapter;
import com.ubaer.zsycz.ui.base.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 已处理订单
 * Created by Administrator on 2018/9/25 0025.
 */

public class AlreadyProcessedFragment extends BaseFragment<UntreatedPresenter> implements PullToRefreshLayout.OnRefreshListener, UntreatedContract.View {

    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout pullToRefreshLayout;
    @BindView(R.id.listview)
    PullableListView listview;
    AlreadyProcessedAdapter alreadyProcessedAdapter;
    //订单状态  0待处理 1充值成功 2充值失败
    String status = "1";
    int page = Constants.PAGE_START;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_already_processed;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        pullToRefreshLayout.setOnRefreshListener(this);
        listview.setCanPulldown(true);
        alreadyProcessedAdapter = new AlreadyProcessedAdapter(mContext);
        listview.setAdapter(alreadyProcessedAdapter);
        listview.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(mContext, OrderDetailsActivity.class);
            intent.putExtra("id", alreadyProcessedAdapter.getList().get(position).getId());
            startActivity(intent);
        });
        mPresenter.getOrderList(status, "", page, Constants.PAGE_DEFAULT_COUNT + "");
    }

    @Override
    protected void lazyLoadData() {

    }


    @Override
    public void setOrderList(List<OrderModel> list, boolean ispage) {
        if (list == null)
            return;
        listview.setCanPullup(ispage);
        if (page == Constants.PAGE_START) {
            alreadyProcessedAdapter.setData(list);
        } else {
            alreadyProcessedAdapter.addData(list);
        }
        if (alreadyProcessedAdapter.getList().size() == 0) {
            pullToRefreshLayout.setNoData(View.VISIBLE);
        } else {
            pullToRefreshLayout.setNoData(View.INVISIBLE);
        }
    }

    @Override
    public void setProcessingSucceed() {

    }

    @Override
    public void setProcessingSucceeds() {

    }

    @Override
    public void setProcessingfail(String msg) {

    }

    @Override
    public void RefreshSucceed() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void RefreshFail() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.FAIL);
    }
    @Override
    public void getRefresh() {
    }

    @Override
    public void openLogin() {
        OpenLogin();
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = Constants.PAGE_START;
        mPresenter.getOrderList(status, "", page, Constants.PAGE_DEFAULT_COUNT + "");
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mPresenter.getOrderList(status, "", ++page, Constants.PAGE_DEFAULT_COUNT + "");
    }
}
